<div class="users">
    <el-breadcrumb separator-class="el-icon-arrow-right" class="bread">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 查询和添加 -->
    <el-row :gutter="20">
    <el-col :span="8">
    <!-- 查询 -->
    <el-input placeholder="请输入搜索内容" v-model="searchText" class="input-with-select">
        <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
    </el-input>
    </el-col>
    <el-col :span="4">
        <el-button type="success" plain @click="showUserAddDialog">添加用户</el-button>
      </el-col>
  </el-row>

    <!-- 表格组件 -->
    <el-table
      :data="usersList"
      style="width: 100%">
      <el-table-column
        prop="username"
        label="姓名"
        width="180">
      </el-table-column>
      <el-table-column
        prop="email"
        label="邮箱"
        width="180">
      </el-table-column>
      <el-table-column
        prop="mobile"
        label="电话"
        width="180">
      </el-table-column>
      <el-table-column
        prop="mg_state"
        label="用户状态"
        width="180">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.mg_state" @change="changeUserState(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="280">
        <template slot-scope="scope">
          <!-- 编辑 -->
          <el-button type="primary" plain icon="el-icon-edit" size="mini" @click="showUserEditDialog(scope.row)"></el-button>
          <!-- 删除 -->
          <el-button type="danger" plain icon="el-icon-delete" size="mini" @click="delUser(scope.row.id)">删除</el-button>
          <!-- 分配角色 -->
          <el-button type="success" plain icon="el-icon-check" size="mini" @click="showUserRole(scope.row)">分配角色</el-button>
        </template>
      </el-table-column>
    </el-table>

     <!-- 分页组件 -->
     <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="pagesize"
      :current-page="pagenum"
      @current-change="changePage"
      >
    </el-pagination>
    <!-- 添加用户对话框 -->
    <el-dialog title="添加用户" :visible.sync="isShowUserAddDialog" @close="closeUserAddDialog">
        <el-form :model="userAddForm" label-width="100px" :rules="userAddRules"  ref="userAddFormRef">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="userAddForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="userAddForm.password"></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="userAddForm.email"></el-input>
            </el-form-item>
            <el-form-item label="手机" prop="mobile">
              <el-input v-model="userAddForm.mobile"></el-input>
            </el-form-item>
          </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="isShowUserAddDialog = false">取 消</el-button>
          <el-button type="primary" @click="addUser">确 定</el-button>
        </div>
      </el-dialog>

      <!-- 编辑用户对话框 -->
      <el-dialog title="编辑用户" :visible.sync="isShowUserEditDialog">
          <el-form :model="userEditForm" label-width="80px">
            <el-form-item label="用户名">
                <el-tag type="info">{{ userEditForm.username }}</el-tag>
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="userEditForm.email"></el-input>
            </el-form-item>
            <el-form-item label="手机">
              <el-input v-model="userEditForm.mobile"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="isShowUserEditDialog = false">取 消</el-button>
            <el-button type="primary" @click="editUser">确 定</el-button>
          </div>
        </el-dialog>

        <!-- 分配角色对话框 -->
  <el-dialog title="分配角色" :visible.sync="isShowUserRoleDialog">
    <el-form :model="userRoleForm" label-width="80px">
      <el-form-item label="用户名">
        <el-tag type="info">{{ userRoleForm.username }}</el-tag>
      </el-form-item>
      <el-form-item label="角色列表">
        <!--
          el-select 下拉框组件
            v-model="userRoleForm.rid" 用来获取下拉框组件中选中项的 value 值

          el-option 下拉框选项组件
            label="区域一" 选项名称（给用户看）
            value="shanghai" 选项的值（给程序使用）
        -->
        <el-select v-model="userRoleForm.rid" placeholder="请选择角色">
          <el-option
            v-for="item in rolesList"
            :key="item.id"
            :label="item.roleName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="isShowUserRoleDialog = false">取 消</el-button>
      <el-button type="primary" @click="assignRole">确 定</el-button>
    </div>
  </el-dialog>
</div>
